const { joinPackModule } = require('../modules/joinPackModule');
const { thingListModel } = require('../modules/thingListModule');
//保存入库
async function addJoinPack(req, res, next) {
    const data = req.body;
    const result = await joinPackModule.create(data);
    const result1 = await thingListModel.updateMany({ _id: { $in: result.thingListId }},{joinPackName:data.joinPackName,joinPackMaterial:data.joinPackMaterial});
    const isRun = await thingListModel.updateMany({ _id: { $in: result.thingListId } }, { thingState: true })
    if (result) {
        res.send({
            message: '添加成功',
            code: 200
        })
    } else {
        res.send({
            message: '添加失败',
            code: 500
        })
    }
}
//获取数据
async function getJoinPack(req, res, next) {
    const result = await joinPackModule
        .find()
        .populate('thingListId');
    if (result.length > 0) {
        res.send({
            message: '获取成功',
            code: 200,
            result
        })
    }
}
//删除
async function outJoinPack(req, res, next) {
    const { _id, _id1 } = req.body;
    const result = await joinPackModule.updateOne({ _id }, {
        $pull: {
            'thingListId': _id1
        }
    });
    const isRun = await thingListModel.updateOne({ _id: _id1 }, { thingState: false })
    if (result.nModified > 0) {
        res.send({
            message: '删除成功',
            code: 200,
        })
    } else {
        res.send({
            message: '删除失败',
            code: 500
        })
    }
}
module.exports = {
    addJoinPack, getJoinPack, outJoinPack
}